package com.diaohw.module.system.service.impl;

import com.diaohw.module.system.mapper.PermissionMapper;
import com.diaohw.module.system.obj.dto.PermissionDto;
import com.diaohw.module.system.obj.entity.Permission;
import com.diaohw.module.system.obj.mapstruct.PermissionMapstruct;
import com.diaohw.module.system.obj.query.PermissionQuery;
import com.diaohw.module.system.obj.vo.PermissionVo;
import com.diaohw.module.system.service.PermissionService;
import com.diaohw.platform.common.obj.page.PageResultVo;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 系统权限 服务实现类
 * </p>
 *
 * @author D-Hw
 * @since 2024-07-05
 */
@Service
public class PermissionImpl implements PermissionService {

    @Resource
    private PermissionMapper permissionMapper;

    @Override
    public PermissionVo create(PermissionDto data) {
        Permission model = PermissionMapstruct.INSTANCE.dtoToEntity(data);
        permissionMapper.insert(model);
        return PermissionMapstruct.INSTANCE.entityToVo(model);

    }

    @Override
    public PermissionVo update(String id, PermissionDto data) {
        Permission model = PermissionMapstruct.INSTANCE.dtoToEntity(data);
        model.setId(id);
        permissionMapper.update(model);
        return PermissionMapstruct.INSTANCE.entityToVo(model);
    }

    @Override
    public Boolean delete(String id) {
        return permissionMapper.deleteById(id) > 0;
    }

    @Override
    public PermissionVo detail(String id) {
        return PermissionMapstruct.INSTANCE.entityToVo(permissionMapper.selectOneById(id));
    }

    @Override
    public List<PermissionVo> findByList(PermissionQuery query) {
        QueryWrapper wrapper = PermissionMapper.createWrapper(query);
        return PermissionMapstruct.INSTANCE.entityToVo(permissionMapper.selectListByQuery(wrapper));
    }

    @Override
    public PageResultVo<PermissionVo> findByPage(PermissionQuery query) {
        QueryWrapper wrapper = PermissionMapper.createWrapper(query);

        Page<Permission> page = permissionMapper.paginate(query.getPageIndex(), query.getPageSize(), wrapper);
        return PageResultVo.create(page.getTotalRow(), PermissionMapstruct.INSTANCE.entityToVo(page.getRecords()));
    }


}
